﻿Public Class frmInputKHRType
    Private khrDa As New DSVaultingTableAdapters.khr_sheetsTableAdapter
    Private action As String
    Private actType As String() = {"NONE", "INSERT", "UPDATE", "DELETE"}
    Private slcVal As Long

    Private Sub frmInputNoteType_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmSetting.Enabled = True
    End Sub

    Private Sub frmInputNoteType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.updateDGV()
    End Sub

    ' Insert new note into database

    Private Sub uxInputNewNotbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxInputNewNotbtn.Click
        uxInputNotTypetbx.Enabled = True
        uxInputNotTypetbx.Text = ""
        action = actType(1)
    End Sub

    ' Edit note upon changes in datagrid

    Private Sub uxEditNotebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxEditNotebtn.Click
        If uxNoteTypedgv.SelectedRows.Count > 0 Then
            uxInputNotTypetbx.Enabled = True
            uxInputNotTypetbx.Text = uxNoteTypedgv.SelectedRows.Item(0).Cells(1).Value
            If Not uxInputNotTypetbx.Text = "" Then
                slcVal = uxInputNotTypetbx.Text
                action = actType(2)
            End If
        End If
    End Sub

    Private Sub uxDeleteNotebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxDeleteNotebtn.Click
        If MessageBox.Show("តើអ្នកពិតជាចង់លុបចោលទិន្នន័យនេះមែនទេ?", "សារបញ្ជាក់ពីការលុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
            Dim oldId As Long = uxNoteTypedgv.SelectedRows.Item(0).Cells(0).Value
            khrDa.Delete(oldId, uxNoteTypedgv.SelectedRows.Item(0).Cells(1).Value)
            Me.updateDGV()
        End If
    End Sub

    Private Sub uxInputNoteSavebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxInputNoteSavebtn.Click
        Dim noteVal As Integer = If(uxInputNotTypetbx.Text = "", 0, uxInputNotTypetbx.Text)
        If noteVal > 0 And action = actType(1) Then
            khrDa.Insert(noteVal)
            'Dim dr As DataRow = khrDt.NewRow
            'dr(1) = noteVal
            'khrDt.Rows.Add(dr)
            Me.updateDGV()
            uxInputNotTypetbx.Clear()
        ElseIf noteVal > 0 And action = actType(2) Then
            If MessageBox.Show("តើអ្នកពិតជាចងរក្សាទុកព័ត៌មានដែលបានកែប្រែមែនទេ?", "សារបញ្ជាក់ពីការកែប្រែ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                Dim oldId As Long = uxNoteTypedgv.SelectedRows.Item(0).Cells(0).Value
                khrDa.Update(noteVal, oldId, slcVal)
                Me.updateDGV()
            End If
        Else
            MsgBox("Please input some value.")
        End If
    End Sub

    Private Sub updateDGV()
        uxNoteTypedgv.DataSource = khrDa.GetData
    End Sub

    ' Close or cancel

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        frmSetting.Enabled = True
        Me.Close()
    End Sub

    ' Double click to edit

    Private Sub uxNoteTypedgv_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles uxNoteTypedgv.CellDoubleClick
        uxEditNotebtn_Click(sender, e)
    End Sub

    Private Sub uxInputNotTypetbx_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles uxInputNotTypetbx.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
            uxInputNoteSavebtn_Click(sender, e)
        End If
    End Sub
End Class